<?php
namespace app\controller;

use app\model\TasksModel;

class TasksControllers
{

  const VIEW_PATH = APP_PATH . 'app/views/Tasks/';
  var $username;
  var $message = "";
  var $messagecolor = "";
  private $task;
  var $canAccept;
  /**
   * 构造函数
   * 
   * 开启会话存或读数据
   */
  public function __construct()
  {
    session_start();
    $this->task = new TasksModel();
    $this->canAccept = $_SESSION['canAccept'];
    $this->username = $_SESSION['username'];
  }
  /**
   * 显示方案详情
   * 
   * $id 方案 id
   * $username 用户名
   * $canAccept 是否能接受这个任务(判断用户是否有任务了，判断这个任务有没有负责人)
   * $canGiveUp 有接受这个任务吗，有才能放弃任务
   * $canDelete 是任务的发布者吗，是才能删除任务
   * $canDone   是任务的发布者吗，是才能完成任务
   *
   * @return void
   */
  public function detailAction()
  {

    $title = '任务详情';
    $id = $_GET['id'];
    $canGiveUp = false;
    $canDelete = false;
    $canDone = false;
    $canChange = false;

    $detailResult = $this->task->showDetail($id);
    $taskTitle = $detailResult[0][1];
    $taskInfo = $detailResult[0][2];
    $taskAccept = $detailResult[0][3];
    $taskInput = $detailResult[0][4];
    $taskDone = $detailResult[0][5];

    //修改任务的权限
    if ($taskAccept === $this->username) {
      $canGiveUp = true;
    }
    if ($taskInput === $this->username) {
      $canDelete = true;
      $canDone = true;
    }
    if ($taskDone === "0") {
      $canChange = true;
    }
    if ($taskAccept !== null) {
      $_SESSION['canAccept'] = false;
    } else {
      $canDone = false;
    }

    include static::VIEW_PATH . 'detail.php';

  }

  /**
   * 显示所有任务
   */
  public function allAction()
  {
    // $username = $_SESSION['username'];
    // $canAccept = $_SESSION['canAccept'];
    $result = $this->task->showAll();
    $titlename = "全部页面";
    include static::VIEW_PATH . 'all.php';
  }

  /**
   * 显示项目进度
   */
  public function progressAction()
  {
    $result = $this->task->showProgress();
    $titlename = "项目进度";
    $taskTotal = count($result);
    $userDone = array();

    // 计算每个人的任务完成个数
    foreach ($result as $key => $value) {
      if ($value[0] !== null) {
        if (!isset($userDone[$value[0]])) {
          $userDone[$value[0]] = 0;
        }
        if ($value[1] === "1") {
          $userDone[$value[0]]++;
        }
      }
    }
    $taskDone = array_sum($userDone);
    
    // //转换成百分比
    // foreach ($userDone as $key => &$value) {
    //   $value = round($value / $taskTotal, 2) * 100
    // }
    if (isset($userDone[$this->username])) {
      $myDone = $userDone[$this->username];
      $myDone = round($myDone / $taskTotal, 2) * 100;
    }else{
      $myDone = 0;
    }
    
    $userDone[$this->username] = 0;
    $otherDone = array_sum($userDone);
    $otherDone = round($otherDone / $taskTotal, 2) * 100;
    $notDone = $taskTotal-$taskDone;
    $notDonePresent = 100 - $myDone - $otherDone . "%";
    $myDone .= "%";
    $otherDone .= "%";
    include static::VIEW_PATH . 'progress.php';
  }


  /**
   * 接受任务
   */
  public function acceptAction()
  {
    $id = $_GET['id'];
    var_dump($_SERVER['PATH_INFO']);
    $canChange = $_GET['change'];
    $titlename = "接受任务";
    if ($this->canAccept && $canChange) {
      $result = $this->task->acceptTask($id, $this->username);
      if ($result === true) {
        $this->message = "任务已接受,点击返回任务页面";
        // $_SESSION['canAccept'] = false;
        $this->messagecolor = "success";
        $this->canAccept = false;
      } else {
        $this->message = "发生未知错误,请点击返回后重试";
        $this->messagecolor = "error";
      }
    } else {
      $this->message = "你无法接受此任务,可能原因是该任务已被接受或完成,或者是你已经有任务了";
      $this->messagecolor = "error";
    }
    include static::VIEW_PATH . "crud.php";

  }

  /**
   * 删除任务
   */
  public function deleteAction()
  {
    $titlename = "删除任务";
    $id = $_GET['id'];
    $canChange = $_GET['change'];
    $canDelete = $_GET['delete'];
    if ($canDelete && $canChange) {
      $result = $this->task->deleteTask($id);
      if ($result === true) {
        $this->message = "任务已删除,点击返回任务页面";
        $this->messagecolor = "success";
      } else {
        $this->message = "发生未知错误,请点击返回后重试";
        $this->messagecolor = "error";
      }
    } else {
      $this->message = "你无法删除此任务,可能原因是你不是该任务发布者或者是该任务已经被完成";
      $this->messagecolor = "error";
    }
    include static::VIEW_PATH . "crud.php";
  }

  /**
   * 放弃任务
   *
   */
  public function giveUpAction()
  {
    $titlename = "放弃任务";
    $id = $_GET['id'];
    $canChange = $_GET['change'];
    $canGiveUp = $_GET['giveup'];
    if ($canGiveUp && $canChange) {
      $result = $this->task->giveUpTask($id);
      if ($result === true) {
        $this->message = "任务已放弃,点击返回任务页面";
        // $_SESSION['canAccept'] = true;
        $this->messagecolor = "success";
        $this->canAccept = true;
      } else {
        $this->message = "发生未知错误,请点击返回后重试";
        $this->messagecolor = "error";
      }
    } else {
      $this->message = "你无法放弃此任务,可能原因是你没有接受此任务或者是该任务已经被完成";
      $this->messagecolor = "error";
    }
    include static::VIEW_PATH . "crud.php";
  }

  /**
   * 完成任务
   */
  public function doneAction()
  {
    $titlename = "完成任务";
    $id = $_GET['id'];
    $canChange = $_GET['change'];
    $canDone = $_GET['done'];
    if ($canDone && $canChange) {
      $result = $this->task->doneTask($id);
      if ($result === true) {
        $this->message = "任务已完成,点击返回任务页面";
        // $_SESSION['canAccept'] = true;
        $this->messagecolor = "success";
        $this->canAccept = true;
      } else {
        $this->message = "发生未知错误,请点击返回后重试";
        $this->messagecolor = "error";
      }
    } else {
      $this->message = "无法结束任务,可能原因是你不是该任务的发布者或者该任务已经被完成或着该任务无人负责";
      $this->messagecolor = "error";
    }
    include static::VIEW_PATH . "crud.php";
  }

  //发布任务页面
  public function inputAction()
  {
    $titlename = "发布任务";
    include static::VIEW_PATH . "input.php";
  }

  //发布任务
  public function inputSuccessAction()
  {
    $titlename = "发布任务";
    $error = "";
    $param = array();
    $param['task_title'] = $_POST['taskTitle'];
    $param['task_info'] = $_POST['taskInfo'];
    $param['task_input'] = $this->username;
    foreach ($param as $key => $value) {
      if (stristr($value, '"')) {
        $error = true;
        break;
      }
    }
    if (!$error) {
      $result = $this->task->inputTask($param);
      if ($result === true) {
        $this->message = "任务发布成功,点击返回任务页面";
        $this->messagecolor = "success";
      } else {
        $this->message = "发生未知错误,请点击返回后重试";
        $this->messagecolor = "error";
      }
    } else {
      $this->message = "发布失败,请使用单引号代替双引号";
      $this->messagecolor = "error";
    }

    include static::VIEW_PATH . "crud.php";
  }


}